package com.alibaba.algo.apiimpl.user;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.usercomponent.service.user.SysUserRoleService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectRoleListByUserRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectUserRoleRequestVO;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.domain.user.SysUserRoleDomain;
import com.alibaba.algo.entity.user.SysUserRole;
import com.alibaba.algo.exception.BaseErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 类SysUserRoleServiceImpl.java的实现描述：用户角色关联 serviceimpl
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Slf4j
@RestController
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleDomain sysUserRoleDomain;

    @Override
    public void insert(Long userId, Long roleId) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        sysUserRole.setRoleId(roleId);
        sysUserRoleDomain.insert(sysUserRole);
    }

    /**
     * 删除用户的角色信息
     *
     * @param userId 用户标识
     */
    @Override
    public void delete(Long userId) {
        if (null == userId || userId <= 0) {
            throw new BaseException(BaseErrorCode.USER_ID_IS_NULL.getCode(), BaseErrorCode.USER_ID_IS_NULL.getMessage());
        }
        SysUserRole sysUserRoleCon = new SysUserRole();
        sysUserRoleCon.setUserId(userId);
        sysUserRoleCon.setIsDeleted(YesOrNo.Y.name());
        sysUserRoleCon.setGmtModified(LocalDateTime.now());
        sysUserRoleDomain.updateById(sysUserRoleCon);
    }

    @Override
    public void updateIsDeletedByUserIdLastModified(Long userId, LocalDateTime lastModifyTime) {
        if (null == userId || userId <= 0) {
            throw new BaseException(BaseErrorCode.USER_ID_IS_NULL.getCode(), BaseErrorCode.USER_ID_IS_NULL.getMessage());
        }
        SysUserRole sysUserRoleCon = new SysUserRole();
        sysUserRoleCon.setUserId(userId);
        sysUserRoleCon.setIsDeleted(YesOrNo.N.name());
        sysUserRoleCon.setGmtModified(LocalDateTime.now());
        sysUserRoleDomain.updateWithIsDeletedByUserIdLastModified(sysUserRoleCon,lastModifyTime);
    }

    @Override
    public void update(Long userId, Long roleId) {
        SysUserRole sysUserRoleCon = new SysUserRole();
        sysUserRoleCon.setUserId(userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleDomain.listByPara(sysUserRoleCon, new LinkedHashMap<>());
        boolean delFlag = true;
        for (SysUserRole sysUserRole : sysUserRoleList) {
            Long rId = sysUserRole.getRoleId();
            if (rId.equals(roleId)) {
                delFlag = false;
            }
        }
        if (delFlag == true) {
            for (SysUserRole sysUserRole : sysUserRoleList) {
                sysUserRole.setIsDeleted(YesOrNo.Y.name());
                sysUserRoleDomain.updateById(sysUserRole);
            }
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            sysUserRoleDomain.insert(userRole);
        }
    }

    /**
     * 角色用户分页
     *
     * @param vo 查询角色下的用户列表
     * @return 用户列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public PagerEntity selectPage(SelectUserRoleRequestVO vo) {
        com.alibaba.algo.core.base.PagerEntity pagerEntity = sysUserRoleDomain.selectPage(vo);
        return new PagerEntity(pagerEntity.getTotal(), pagerEntity.getTotalPages(), pagerEntity.getPageSize(), pagerEntity.getPageIndex(), pagerEntity.getDataList());
    }

    @Override
    public BaseResult selectRoleCodeByUser(SelectRoleListByUserRequestVO vo){
        return BaseResult.buildSuccess(sysUserRoleDomain.selectRoleCodeByUser(vo));
    }
}
